<?php
/**
 * V7 checked 1
 *
 */
class data_circle extends data {
	public function __construct() {
		parent::__construct();
	}
	/**
	 * 列出用户的圈子
	 *
	 * @param int $uid
	 * @param string $type			// all / member / follow
	 * @param string $relation
	 * @return array
	 */
	public function ls ($uid, $type='all/member/follow', $relation='public') {
		$uid 		= intval($uid);
		$type		= ($type === 'member' or $type === 'follow') ? $type : 'all';
		$relation 	= ($relation === 'private') ? $relation : 'public';
		
		if ($uid) {
			$execute = $this->db->execute('
				select
					circle.id			AS id,
					circle.title			AS title,
					circle.privacy		AS privacy,
					circle.total_article		AS total_article,
					circle.total_member	AS total_member,
					circle.total_follower	AS total_follower,
					circle_member.type	AS member_type,
					circle_member.total_articles_by_me	AS total_articles_by_me
				from
					circle, circle_member
				where
					circle_member.user_id 	= "'.$uid.'"					and
					'.(($type === 'member') ? ' (circle_member.type = "member" or circle_member.type = "admin" or circle_member.type = "founder") and ' : (($type === 'follow') ? ' circle_member.type = "follower" and ' : '')).'
					circle.id			= circle_member.circle_id		and
					'.(($relation==='private') ? ' 1 ' : ' circle.privacy="public" ').'
				order by
					circle_member.type			DESC, 
					circle_member.total_articles_by_me	DESC,
					circle_member.id				ASC
			');
			return $execute;
		} else return false;
	}
	/**
	 * 列出我的圈子，不包括article_id的圈子
	 *
	 * @param int $uid
	 * @param int $article_id
	 * @return array
	 */
	public function ls_without_article ($uid, $article_id) {
		$uid		= intval($uid);
		$article_id	= intval($article_id);
		
		if ($uid and $article_id) {
			$execute = $this->db->execute('
				select
					circle.*
				from
					circle_member, circle
				where
					circle_member.user_id	= "'.$uid.'"	and
					(circle_member.type = "member" or circle_member.type = "admin" or circle_member.type = "founder")	and
					circle_member.circle_id NOT IN (
						select DISTINCT(circle_id) from circle_article where article_id = "'.$article_id.'"
					)	and
					circle.id	= circle_member.circle_id
			');
			return $execute;
		} else return false;
	}
	/**
	 * 创建圈子
	 * 
	 * @param int $uid
	 * @param string $title				// 没有任何转义的字符串
	 * @param string $privacy
	 * @return int
	 */
	public function add ($uid, $title, $privacy) {
		$uid 		= intval($uid);
		$title		= format_nickname($title);
		$privacy 	= ($privacy === 'private') ? $privacy : 'public';
		
		if ($uid and $title and $privacy) {
			$circle_id = $this->db->execute('
				insert into 
					circle 
				set
					title			= "'.$title.'",
					privacy		= "'.$privacy.'",
					total_article		= 0,
					total_member	= 0,
					total_follower	= 0
			');
			if ($circle_id) {
				$insert_founder = $this->db->execute('
					insert into
						circle_member
					set
						circle_id		= "'.$circle_id.'",
						user_id		= "'.$uid.'",
						type			= "founder"
				');
				if ($insert_founder) {
					// 统计成员数
					$this->count_member($circle_id, 1);

					// 发布event
					new data_event('create_circle', array ('user_id'=>$uid,'circle_id'=>$circle_id), 'low');
					return $circle_id;
				} else {
					$this->db->execute('
						delete from
							circle
						where
							id = "'.$circle_id.'"
					');
					return false;
				}
			} else return false;
		} else return false;
	}
	/**
	 * 圈子的基本信息
	 * 			检查隐私设置
	 *
	 * @param int $uid			// uid 可以为0
	 * @param int $id			// circle id
	 * @return array
	 */
	public function one ($uid, $id) {
		$uid		= intval($uid);
		$id		= intval($id);
		
		if ($id) {
			$execute = $this->db->execute('
				select
					*
				from
					circle
				where
					id		= "'.$id.'"
			');
			if ($execute) {
				if ($execute[0]['privacy'] == 'public' or $this->is_member($uid, $id)) {
					return $execute[0];
				} else {
					return false;
				}
			} else return false;
		} else return false;
	}
	/**
	 * 修改圈子标题和隐私
	 * 		发布event
	 *
	 * @param int $uid
	 * @param int $circle_id
	 * @param string $title				// 没有任何转义
	 * @param string $privacy
	 * @return bool
	 */
	public function edit ($uid, $circle_id, $title, $privacy) {
		$uid		= intval($uid);
		$circle_id	= intval($circle_id);
		$title		= format_nickname($title);
		$privacy	= ($privacy === 'private') ? 'private' : 'public';
		
		if ($uid and $circle_id and $title) {
			if ($this->is_founder($uid, $circle_id)) {
				// 查询原来的privacy
				$execute = $this->db->execute('
					select
						privacy
					from
						circle
					where
						id	= "'.$circle_id.'"
				');
				// 修改
				$update = $this->db->execute('
					update
						circle
					set
						title		= "'.$title.'",
						privacy	= "'.$privacy.'"
					where
						id	= "'.$circle_id.'"
				');
				if ($update) {
					// 当从public改成private时，删除follower
					if ($privacy == 'private' and $execute[0]['privacy'] == 'public') {
						$this->db->execute('
							delete from
								circle_member
							where
								circle_id	= "'.$circle_id.'"	and
								type		= "follower"
						');
					}
					
					return $update;
				} else return false;
			} else return false;
		} else return false;
	}
	/**
	 * 添加一个圈子成员
	 * 		更新成员总数
	 * 		如果以前订阅过这个circle，那么需要删除订阅
	 * 		发布feed
	 *
	 * @param int $inviter_uid
	 * @param int $accepter_uid
	 * @param int $circle_id
	 * @param bool $bypass_inviter // 跳过检查inviter的身份，在系统自动添加的时候用到此参数
	 * @return int
	 */
	public function add_member ($inviter_uid, $accepter_uid, $circle_id, $bypass_inviter=FALSE) {
		$inviter_uid		= intval($inviter_uid);
		$accepter_uid	= intval($accepter_uid);
		$circle_id		= intval($circle_id);
		
		// 检查一下inviter是否有权限，inviter与accepter是不是好友
		$data_friend = new data_friend();
		if ($accepter_uid and $circle_id and ($bypass_inviter or ($this->is_member($inviter_uid, $circle_id) and $data_friend->is_friend($inviter_uid, $accepter_uid)))) {
			// 检查accepter是否已经是成员或者已经follow了
			$type = $this->get_access($accepter_uid, $circle_id);
			if ($type === false) {				// 没有记录，可以写入
				$insert_id = $this->db->execute('
					insert into
						circle_member
					set
						circle_id		= "'.$circle_id.'",
						user_id		= "'.$accepter_uid.'",
						type			= "member"
				');
				if ($insert_id) {
					// 统计成员数
					$this->count_member($circle_id, 1);
				}
			} elseif ($type === 'follower') {	// 已经订阅过
				$update = $this->db->execute('
					update
						circle_member
					set
						type = "member"
					where
						user_id	= "'.$accepter_uid.'"		and
						circle_id	= "'.$circle_id.'"
				');
				if ($update) {
					// 统计成员数
					$this->count_follower($circle_id, -1);
					$this->count_member($circle_id, 1);
				}
			} else {							// 已经是成员了
				return false;
			}
			if ($insert_id or $update) {
				// 发布event
				new data_event('join_circle', array ('user_id'=>$accepter_uid,'circle_id'=>$circle_id), 'low');

				return true;
			} else return false;
		} else return false;
	}
	/**
	 * 圈子中的成员列表
	 * 		转换了nickname
	 *
	 * @param int $uid		// uid 可以为0
	 * @param int $circle_id
	 * @param int $page		// 最小为1
	 * @param int $limit		// 可以为0
	 * @return array
	 */
	public function list_member ($uid, $circle_id, $page=1, $limit=100) {
		$uid		= intval($uid);
		$circle_id 	= intval($circle_id);
		$page	= ($page>1) ? intval($page) : 1;
		$limit		= intval($limit);
		
		if ($circle_id and $this->one($uid, $circle_id)) {
			$execute = $this->db->execute('
				select
					circle_member.id				AS id,
					circle_member.circle_id			AS circle_id,
					circle_member.user_id			AS user_id,
					circle_member.type			AS type,
					circle_member.total_articles_by_me	AS total_articles_by_me,
					users.nickname				AS nickname,
					users.weibo_id				AS weibo_id,
					users.renren_id				AS renren_id,
					users.kaixin001_id			AS kaixin001_id,
					users.qqwb_id				AS qqwb_id
				from
					circle_member, users
				where
					circle_member.circle_id		= "'.$circle_id.'"			and
					(circle_member.type = "founder" or circle_member.type = "admin" or circle_member.type = "member")	and
					users.id				= circle_member.user_id
				order by
					circle_member.type			DESC, 
					circle_member.total_articles_by_me	DESC,
					circle_member.id				ASC
				'.(($limit) ? ' limit '.(($page-1)*$limit).', '.$limit.' ' : '').'
			');
			if (is_array($execute)) {
				$data_user = new data_user();
				foreach ($execute as $k=>$v) {
					$execute[$k]['friend_nickname'] = $data_user->friend_to_nickname($uid, $v['user_id']);
				}
				return $execute;
			} else return false;
		} else return false;
	}
	/**
	 * 将圈子成员删除，即改成了follower
	 * 		founder和admin权限
	 * 		founder 不能删除
	 * 		更新总数:member, follower
	 *
	 * @param int $uid
	 * @param int $circle_id
	 * @param string $uid_str			// circle member表中的uid，用“,”分隔开
	 * @return bool					// 有一条成功即返回成功
	 */
	public function remove_member ($uid, $circle_id, $uid_str) {
		$uid			= intval($uid);
		$circle_id		= intval($circle_id);
		$uid_str 		= format_id_string($uid_str, false);		// 返回字符串
		
		if ($uid and $circle_id and $uid_str) {
			// 检查uid是否拥有操作权限
			if ($this->is_admin($uid, $circle_id)) {
				// 删除成员
				$update = $this->db->execute('
					update
						circle_member
					set
						type = "follower"
					where
						circle_id	= "'.$circle_id.'"	and
						user_id	IN ('.$uid_str.')	and
						(type = "member" or type = "admin")
				');
				// 更新成员总数
				if ($update) {
					$this->count_member($circle_id);
					$this->count_follower($circle_id);
				}

				return $update;
			} else return false;
		} else return false;
	}
	/**
	 * 将指定成员设定为管理员或者解除管理员
	 * 			验证了操作权限founder或者admin
	 * 			创始人的记录不会被修改
	 *
	 * @param int $uid
	 * @param int $circle_id
	 * @param string $uid_str				// circle member表中的uid，用“,”分隔开
	 * @param bool $to_admin
	 * @return bool							// 有一条成功即返回成功
	 */
	public function set_member_admin ($uid, $circle_id, $uid_str, $to_admin) {
		$uid			= intval($uid);
		$circle_id		= intval($circle_id);
		$uid_str		= format_id_string($uid_str, false);		// 返回字符串
		
		if ($uid and $circle_id and $uid_str) {
			// 检查uid是否拥有操作权限
			if ($this->is_admin($uid, $circle_id)) {
				if ($to_admin) {			// 从member改成admin
					$update = $this->db->execute('
						update
							circle_member
						set
							type = "admin"
						where
							user_id	IN ('.$uid_str.')		and
							circle_id	= "'.$circle_id.'"		and
							type		= "member"
					');
				} else {					// 从admin改成member
					$update = $this->db->execute('
						update
							circle_member
						set
							type = "member"
						where
							user_id	IN ('.$uid_str.')		and
							circle_id	= "'.$circle_id.'"		and
							type		= "admin"
					');
				}
				return $update;
			} else return false;
		} else return false;
	}
	/**
	 * 成员(admin / member)自己退出
	 *
	 * @param int $uid
	 * @param int $circle_id
	 * @return int
	 */
	public function exit_member ($uid, $circle_id) {
		$uid		= intval($uid);
		$circle_id	= intval($circle_id);
		
		if ($uid and $circle_id) {
			$del = $this->db->execute('
				delete from
					circle_member
				where
					user_id	= "'.$uid.'"			and
					circle_id	= "'.$circle_id.'"		and
					(type = "admin" or type = "member")
			');
			if ($del) {
				// 更新成员总数
				$this->count_member($circle_id, -1);

				return $del;
			} else return false;
		} else return false;
	}
	public function add_follower ($follower_id, $circle_id) {
		$follower_id	= intval($follower_id);
		$circle_id		= intval($circle_id);
		
		// 检查一下circle是否public
		if ($follower_id and $circle_id and $this->is_public($circle_id)) {
			$access = $this->get_access($follower_id, $circle_id);
			if (!$access) {		// 不是成员、没有follow过，可以写入
				$insert_id = $this->db->execute('
					insert into
						circle_member
					set
						circle_id		= "'.$circle_id.'",
						user_id		= "'.$follower_id.'",
						type			= "follower"
				');
				if ($insert_id) {
					// 统计数
					$this->count_follower($circle_id, 1);
					
					// 发布event
					new data_event('follow_circle', array ('user_id'=>$follower_id,'circle_id'=>$circle_id), 'low');
					
					return $insert_id;
				} else return false;
			} else return false;
		} else return false;
	}
	public function exit_follower ($uid, $circle_id) {
		$uid		= intval($uid);
		$circle_id	= intval($circle_id);
		
		if ($uid and $circle_id) {
			$del = $this->db->execute('
				delete from
					circle_member
				where
					user_id	= "'.$uid.'"		and
					circle_id	= "'.$circle_id.'"	and
					type		= "follower"
			');
			if ($del) {
				// 更新成员总数
				$this->count_follower($circle_id, -1);

				return $del;
			} else return false;
		} else return false;
	}
	/**
	 * 获取用户对于circle的权限
	 * 
	 * @param int $uid
	 * @param int $circle_id
	 * @return String				// 'follower','member','admin','founder' / FALSE
	 */
	public function get_access ($uid, $circle_id) {
		$uid		= intval($uid);
		$circle_id	= intval($circle_id);
		
		if ($uid and $circle_id) {
			$execute = $this->db->execute('
				select
					*
				from
					circle_member
				where
					user_id	= "'.$uid.'"		and
					circle_id	= "'.$circle_id.'"
				limit
					1
			');
			if ($execute) {
				return $execute[0]['type'];
			} else return false;
		} else return false;
	}
	/**
	 * 用户是否是创始人
	 * 
	 * @param int $uid
	 * @param int $circle_id
	 * @return boolean
	 */
	public function is_founder ($uid, $circle_id) {
		$uid		= intval($uid);
		$circle_id	= intval($circle_id);
		
		if ($uid and $circle_id) {
			$execute = $this->db->execute('
				select
					id
				from
					circle_member
				where
					user_id	= "'.$uid.'"			and
					circle_id	= "'.$circle_id.'"		and
					type 		= "founder"
				limit
					1
			');
			if ($execute) {
				return true;
			} else return false;
		} else return false;
	}
	/**
	 * 用户是否有管理权限，即admin或者founder
	 * 
	 * @param int $uid
	 * @param int $circle_id
	 * @return boolean
	 */
	public function is_admin ($uid, $circle_id) {
		$uid		= intval($uid);
		$circle_id	= intval($circle_id);
		
		if ($uid and $circle_id) {
			$execute = $this->db->execute('
				select
					id
				from
					circle_member
				where
					user_id	= "'.$uid.'"			and
					circle_id	= "'.$circle_id.'"		and
					(type = "admin" or type = "founder")
				limit
					1
			');
			if ($execute) {
				return true;
			} else return false;
		} else return false;
	}
	/**
	 * 是否有圈子成员权限，即member / admin / founder
	 *
	 * @param int $uid
	 * @param int $circle_id
	 * @return bool
	 */
	public function is_member ($uid, $circle_id) {
		$uid		= intval($uid);
		$circle_id	= intval($circle_id);
		
		if ($uid and $circle_id) {
			$execute = $this->db->execute('
				select
					*
				from
					circle_member
				where
					user_id	= "'.$uid.'"		and
					circle_id	= "'.$circle_id.'"	and
					(type = "member" or type = "admin" or type = "founder")
				limit
					1
			');
			if ($execute) {
				return true;
			} else return false;
		} else return false;
	}
	/**
	 * 用户是否是订阅者
	 * 
	 * @param int $uid
	 * @param int $circle_id
	 * @return boolean
	 */
	public function is_follower ($uid, $circle_id) {
		$uid		= intval($uid);
		$circle_id	= intval($circle_id);
		
		if ($uid and $circle_id) {
			$execute = $this->db->execute('
				select
					*
				from
					circle_member
				where
					user_id	= "'.$uid.'"		and
					circle_id	= "'.$circle_id.'"	and
					type 		= "follower"
				limit
					1
			');
			if ($execute) {
				return true;
			} else return false;
		} else return false;
	}
	/**
	 * 圈子是否是公开的
	 * 
	 * @param int $circle_id
	 * @return boolean
	 */
	public function is_public ($circle_id) {
		$circle_id = intval($circle_id);
		if ($circle_id) {
			$execute = $this->db->execute('
				select
					id
				from
					circle
				where
					id 		= "'.$circle_id.'"		and
					privacy	= "public"
			');
			if ($execute) {
				return true;
			} else return false;
		} else return false;
	}
	/**
	 * 统计圈子中的成员总数
	 *
	 * @param int $circle_id
	 * @param int $number
	 * @return int
	 */
	public function count_member ($circle_id, $number=0) {
		$circle_id 	= intval($circle_id);
		$number		= intval($number);
		
		if ($circle_id) {
			if ($number > 0) {
				$count = $this->db->execute('
					update
						circle
					set
						total_member = total_member + '.$number.'
					where
						id = "'.$circle_id.'"
				');
			} elseif ($number < 0) {
				$count = $this->db->execute('
					update
						circle
					set
						total_member = total_member - '.abs($number).'
					where
						id = "'.$circle_id.'"
				');
			} else {
				$count = $this->db->execute('
					update
						circle
					set
						total_member = (select COUNT(*) from circle_member where circle_id="'.$circle_id.'" and (type="member" or type="admin" or type="founder"))
					where
						id = "'.$circle_id.'"
				');
			}
			return $count;
		} else return false;
	}
	/**
	 * 统计圈子的订阅者人数
	 *
	 * @param int $circle_id
	 * @param int $number				// 非0时，直接加减该值
	 * @return int
	 */
	public function count_follower ($circle_id, $number=0) {
		$circle_id 	= intval($circle_id);
		$number	= intval($number);
		
		if ($circle_id) {
			if ($number > 0) {
				$count = $this->db->execute('
					update
						circle
					set
						total_follower = total_follower + "'.$number.'"
					where
						id = "'.$circle_id.'"
				');
			} elseif ($number < 0) {
				$count = $this->db->execute('
					update
						circle
					set
						total_follower = total_follower - "'.abs($number).'"
					where
						id = "'.$circle_id.'"
				');
			} else {
				$count = $this->db->execute('
					update
						circle
					set
						total_follower = (select COUNT(*) from circle_member where circle_id="'.$circle_id.'" and type="follower")
					where
						id = "'.$circle_id.'"
				');
			}
			return $count;
		} else return false;
	}
	/**
	 * 统计圈内文章总数
	 *
	 * @param String $circle_ids	// 可以多个id
	 * @param int $number
	 * @return int
	 */
	public function count_article ($circle_ids, $number=0) {
		$circle_ids		= format_id_string($circle_ids, FALSE);
		$number		= intval($number);
		
		if ($circle_ids) {
			if ($number > 0) {
				$return = $this->db->execute('
					update
						circle
					set
						total_article = total_article + '.$number.'
					where
						id IN ('.$circle_ids.')
				');
			} elseif ($number < 0) {
				$return = $this->db->execute('
					update
						circle
					set
						total_article = total_article - '.abs($number).'
					where
						id IN ('.$circle_ids.')
				');
			} else {
				$circle_ids = format_id_string($circle_ids, true);		// 转成数组
				$return = false;
				foreach ($circle_ids as $circle_id) {
					$count = $this->db->execute('
						update
							circle
						set
							total_article = (select COUNT(*) from circle_article where circle_id = "'.$circle_id.'" and insite_article_id = 0)
						where
							id = "'.$circle_id.'"
					');
					if ($count) $return = true;
				}
			}
			return $return;
		} else return false;
	}
	
	public function count_article_for_me ($uid, $circle_ids, $number=0) {
		$uid		= intval($uid);
		$circle_ids	= format_id_string($circle_ids, FALSE);		// 返回字符串
		$number	= intval($number);
		
		if ($uid and $circle_ids) {
			if ($number > 0) {
				$return = $this->db->execute('
					update
						circle_member
					set
						total_articles_by_me = total_articles_by_me + '.$number.'
					where
						user_id	= "'.$uid.'"			and
						circle_id	IN ('.$circle_ids.')		and
						(type = "member" or type = "admin" or type = "founder")
				');
			} elseif ($number < 0) {
				$return = $this->db->execute('
					update
						circle_member
					set
						total_articles_by_me = total_articles_by_me - '.abs($number).'
					where
						user_id	= "'.$uid.'"			and
						circle_id	IN ('.$circle_ids.')		and
						(type = "member" or type = "admin" or type = "founder")
				');
			} else {
				$circle_ids = format_id_string($circle_ids, true);
				$return = FALSE;
				foreach ($circle_ids as $circle_id) {
					$count = $this->db->execute('
						update
							circle_member
						set
							total_articles_by_me = (select COUNT(*) from circle_article where circle_id = "'.$circle_id.'" and author_id = "'.$uid.'")
						where
							user_id	= "'.$uid.'"			and
							circle_id	= "'.$circle_id.'"		and
							(type = "member" or type = "admin" or type = "founder")
					');
					if ($count) $return = true;
				}
			}
			return $return;
		} else return false;
	}
}
?>